package com.savitar.menu.controller;

import com.savitar.common.vo.ResultBean;
import com.savitar.menu.entity.Menu;
import com.savitar.menu.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * (Menu)控制层
 *
 * @author Savitar
 * @since 2021-05-16 09:28:26
 */
@RestController
@RequestMapping("menu")
public class MenuController {

    @Autowired
    private MenuService service;

    @GetMapping("/tree")
    public ResultBean tree() {
        try {
            return ResultBean.ok(service.tree());
        } catch (Exception e) {
            return ResultBean.err(e.getMessage());
        }
    }

    @GetMapping("/treeByRole/{roleId}")
    public ResultBean treeByRole(@PathVariable String roleId) {
        try {
            return ResultBean.ok(service.treeByRole(roleId));
        } catch (Exception e) {
            return ResultBean.err(e.getMessage());
        }
    }

    @PostMapping
    public ResultBean create(@RequestBody Menu createVo) {
        try {
            return ResultBean.ok(service.create(createVo));
        } catch (Exception e) {
            return ResultBean.err(e.getMessage());
        }
    }

    @PutMapping
    public ResultBean update(@RequestBody Menu updateVo) {
        try {
            return ResultBean.ok(service.update(updateVo));
        } catch (Exception e) {
            return ResultBean.err(e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public ResultBean deleteById(@PathVariable String id) {
        try {
            return ResultBean.ok(service.deleteById(id));
        } catch (Exception e) {
            return ResultBean.err(e.getMessage());
        }
    }

    @GetMapping("/listMenuIdByRole/{roleId}")
    public ResultBean listMenuIdByRole(@PathVariable String roleId) {
        try {
            return ResultBean.ok(service.listMenuIdByRole(roleId));
        } catch (Exception e) {
            return ResultBean.err(e.getMessage());
        }
    }

    @PostMapping("/createRole2Menu/{roleId}")
    public ResultBean createRole2Menu(@PathVariable String roleId,
                                      @RequestBody List<String> menus) {
        try {
            return ResultBean.ok(service.createRole2Menu(roleId, menus));
        } catch (Exception e) {
            return ResultBean.err(e.getMessage());
        }
    }

}